package com.ltcode.log.starter.entity;

import lombok.Data;

import java.util.Stack;

@Data
public class ThreadLocalEntity {
    private Stack<DataEntity> dataEntityStack = new Stack<>();

    public void pushDataEntity(DataEntity dataEntity) {
        dataEntityStack.push(dataEntity);
    }

    public DataEntity popDataEntity() {
        return dataEntityStack.isEmpty() ? null : dataEntityStack.pop();
    }

    public DataEntity peekDataEntity() {
        return dataEntityStack.isEmpty() ? null : dataEntityStack.peek();
    }

    // 检查数据栈是否为空
    public boolean isDataEmpty() {
        return dataEntityStack.isEmpty();
    }

    // 获取栈中数据实体的数量
    public int getDataEntityCount() {
        return dataEntityStack.size();
    }

    // 清空所有数据
    public void clearAllData() {
        dataEntityStack.clear();
    }

    // 获取数据栈
    public Stack<DataEntity> getDataEntityStack() {
        return dataEntityStack;
    }

    // 用于跨线程复制数据
    public static ThreadLocalEntity createCopy(ThreadLocalEntity original) {
        ThreadLocalEntity copy = new ThreadLocalEntity();
        if (original != null && original.getDataEntityStack() != null) {
            for (DataEntity entity : original.getDataEntityStack()) {
                copy.pushDataEntity(entity);
            }
        }
        return copy;
    }
}